package com.avcdata.national.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.main.BaseActivity;
import com.avcdata.aoscustommade.utils.AppActivityManager;
import com.avcdata.aoscustommade.utils.ShowUtils;
import com.avcdata.national.fragment.AreaFragment;
import com.avcdata.national.fragment.AvgPriceFragment;
import com.avcdata.national.fragment.BrandsFragment;
import com.avcdata.national.fragment.HotSaleFragment;
import com.avcdata.national.fragment.ScaleFragment;
import com.avcdata.national.fragment.StructureFragment;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * NewAOSApp
 * com.avcdata.national.activity
 * Created by lic on 2017/8/22.
 * CSDN http://blog.csdn.net/lucasey
 */

public class ScaleBasisRatioActivity extends BaseActivity {

    @ViewInject(R.id.left_group)
    private LinearLayout left_group;
    @ViewInject(R.id.leftText)
    private TextView leftText;
    @ViewInject(R.id.right_group)
    private LinearLayout right_group;
    @ViewInject(R.id.title)
    private TextView title;

    @ViewInject(R.id.scale_group)
    private LinearLayout scale_group;
    @ViewInject(R.id.scale_iv)
    private ImageView scale_iv;
    @ViewInject(R.id.scale_tv)
    private TextView scale_tv;
    @ViewInject(R.id.brands_group)
    private LinearLayout brands_group;
    @ViewInject(R.id.brands_iv)
    private ImageView brands_iv;
    @ViewInject(R.id.brands_tv)
    private TextView brands_tv;
    @ViewInject(R.id.structure_group)
    private LinearLayout structure_group;
    @ViewInject(R.id.structure_iv)
    private ImageView structure_iv;
    @ViewInject(R.id.structure_tv)
    private TextView structure_tv;
    @ViewInject(R.id.avgprice_group)
    private LinearLayout avgprice_group;
    @ViewInject(R.id.avgprice_iv)
    private ImageView avgprice_iv;
    @ViewInject(R.id.avgprice_tv)
    private TextView avgprice_tv;
    @ViewInject(R.id.hotsale_group)
    private LinearLayout hotsale_group;
    @ViewInject(R.id.hotsale_iv)
    private ImageView hotsale_iv;
    @ViewInject(R.id.hotsale_tv)
    private TextView hotsale_tv;
    @ViewInject(R.id.area_group)
    private LinearLayout area_group;
    @ViewInject(R.id.area_iv)
    private ImageView area_iv;
    @ViewInject(R.id.area_tv)
    private TextView area_tv;

    @ViewInject(R.id.jiazai_layout)
    private LinearLayout loading;

    private StructureFragment fragment_str;
    private AvgPriceFragment fragment_avg;
    private HotSaleFragment fragment_hot;
    private BrandsFragment fragment_bra;
    private ScaleFragment fragment_sca;
    private AreaFragment fragment_area;

    private FragmentManager manager;

    private List<TextView> tvList = new ArrayList<>();

    public String category = "";

    public String getCategory(){
        return category;
    }

    public LinearLayout getLoading(){
        return loading;
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_national_scale);
        AppActivityManager.getAppManager().addActivity(this);
        ViewUtils.inject(this);

        fragment_str = new StructureFragment();
        fragment_avg = new AvgPriceFragment();
        fragment_hot = new HotSaleFragment();
        fragment_bra = new BrandsFragment();
        fragment_sca = new ScaleFragment();
        fragment_area = new AreaFragment();

        tvList.add(scale_tv);
        tvList.add(brands_tv);
        tvList.add(structure_tv);
        tvList.add(avgprice_tv);
        tvList.add(hotsale_tv);
        tvList.add(area_tv);
        leftText.setText("返回");
    }

    @Override
    public void initInstall() {
        right_group.setVisibility(View.VISIBLE);
        left_group.setOnClickListener(this);
        right_group.setOnClickListener(this);
        scale_group.setOnClickListener(this);
        brands_group.setOnClickListener(this);
        avgprice_group.setOnClickListener(this);
        structure_group.setOnClickListener(this);
        hotsale_group.setOnClickListener(this);
        area_group.setOnClickListener(this);

        category = getIntent().getStringExtra("cate");
        title.setText("市占-"+category);
    }

    @Override
    public void initData() {
        switchFragment(1);
    }

    @Override
    public void evenControl() {

    }

    @Override
    public void onClickEvent(View view) {
        if(loading.getVisibility() == View.GONE) {
            switch (view.getId()) {
                case R.id.left_group:
                    this.finish();
                    break;
                case R.id.right_group:
                    // TODO: 2017/8/23 分享待接入
                    showShare ( getWindow ().getDecorView ().findViewById ( android.R.id.content ) );
                    break;
                case R.id.scale_group:
                    switchFragment(0);
                    break;
                case R.id.brands_group:
                    switchFragment(1);
                    break;
                case R.id.structure_group:
                    switchFragment(2);
                    break;
                case R.id.avgprice_group:
                    switchFragment(3);
                    break;
                case R.id.hotsale_group:
                    switchFragment(4);
                    break;
                case R.id.area_group:
                    switchFragment(5);
                    break;
                default:
                    break;
            }
        }
    }

    // 具体显示哪一个Fragment
    // 不采用replace替换，使用add，show，hide避免Fragment切换时重新加载网络请求
    public void switchFragment(int index) {
        changeIcon ();
        manager = getSupportFragmentManager ();
        switch (index) {
            case 0:
                title.setText ( "规模-"+category );
                scale_iv.setBackgroundResource ( R.mipmap.scale_true );
                scale_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_sca == null) {
                    fragment_sca = new ScaleFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_sca ).commit ();
                }
                if (!fragment_sca.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_sca ).commit ();
                }
                break;
            case 1:
                title.setText ( "市占-"+category );
                brands_iv.setBackgroundResource ( R.mipmap.brandcomp_true );
                brands_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_bra == null) {
                    fragment_bra = new BrandsFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_bra ).commit ();
                }
                if (!fragment_bra.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_bra ).commit ();
                }
                break;
            case 2:
                title.setText ( "结构-"+category );
                structure_iv.setBackgroundResource ( R.mipmap.structure_true );
                structure_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_str == null) {
                    fragment_str = new StructureFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_str ).commit ();
                }
                if (!fragment_str.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_str ).commit ();
                }
                break;
            case 3:
                title.setText ( "均价-"+category );
                avgprice_iv.setBackgroundResource ( R.mipmap.avgprice_true);
                avgprice_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_avg == null) {
                    fragment_avg = new AvgPriceFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_avg ).commit ();
                }
                if (!fragment_avg.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_avg ).commit ();
                }
                break;
            case 4:
                title.setText ( "型号-"+category );
                hotsale_iv.setBackgroundResource ( R.mipmap.hotsale_true );
                hotsale_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_hot == null) {
                    fragment_hot = new HotSaleFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_hot ).commit ();
                }
                if (!fragment_hot.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_hot ).commit ();
                }
                break;
            case 5:
                title.setText ( "区域-"+category );
                area_iv.setBackgroundResource ( R.mipmap.area_true );
                area_tv.setTextColor ( Color.parseColor ( "#00C2FF" ) );
                if (fragment_area == null) {
                    fragment_area = new AreaFragment ();
                    manager.beginTransaction ().replace ( R.id.container, fragment_area ).commit ();
                }
                if (!fragment_area.isAdded ()) {
                    manager.beginTransaction ().replace ( R.id.container, fragment_area ).commit ();
                }
                break;
        }

    }

    private void changeIcon() {
        for (TextView mtv : tvList) {
            mtv.setTextColor ( getResources ().getColor ( R.color.black ) );
        }
        scale_iv.setBackgroundResource ( R.mipmap.scale_false );
        brands_iv.setBackgroundResource ( R.mipmap.brandcomp_false );
        structure_iv.setBackgroundResource ( R.mipmap.structure_false );
        avgprice_iv.setBackgroundResource ( R.mipmap.avgprice_false );
        hotsale_iv.setBackgroundResource ( R.mipmap.hotsale_false );
        area_iv.setBackgroundResource ( R.mipmap.area_false );
    }

}
